import {expect} from "chai";
import { Mat4 } from "../src/Mat4";
import { Quat } from "../src/Quat";
describe("Mat4", function () {
    before(function() {
        console.log("开始自动化测试Mat4...");
    });

    after(function() {
        console.log("结束...");
    });
    beforeEach(function() {
    });

    afterEach(function() {
    });
    it('#constructor', function () {
        let mat4=new Mat4();
        expect(mat4.equal([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])).to.equal(true);
    });
    it('#clone', function () {
        let mat4=new Mat4();
        let clone=mat4.clone();
        expect(mat4.equal(clone)).to.equal(true);
    });
    it('#equal', function () {
        let mat4=new Mat4();
        let clone=mat4.clone();
        expect(mat4.equal(clone)).to.equal(true);
    });
    it('#identity', function () {
        let mat4=new Mat4([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6]);
        mat4.identity();
        expect(mat4.equal(new Mat4())).to.equal(true);
    });
    it('#transpose', function () {
        let mat4=new Mat4([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6]);
        mat4.transpose();
        expect(mat4.equal([1,5,9,3,2,6,0,4,3,7,1,5,4,8,2,6])).to.equal(true);
    });
    it('#invert', function () {
        let mat4=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        mat4.invert();
        expect(mat4.equal([1,-1,0,-1,0,2,0,0,0,0,1,0,0,0,0,1])).to.equal(true);
    });
    it('#adjoint', function () {
        let mat4=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        mat4.adjoint();
        expect(mat4.equal([0.5,-0.5,0,-0.5,0,1,0,0,0,0,0.5,0,0,0,0,0.5])).to.equal(true);
    });
    it('#determinant', function () {
        let mat4=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        let out=mat4.determinant();
        expect(out).to.equal(0.5);
    });
    it('#multiply', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        let right=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,0.5,0,0,0,0,1]);
        left.multiply(right);
        expect(left.equal([1,0.75,0,2,0,0.25,0,0,0,0,0.5,0,0,0,0,1])).to.equal(true);
    });
    it('#premultiply', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        let right=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,0.5,0,0,0,0,1]);
        right.multiply(left);
        expect(right.equal([1,0.75,0,2,0,0.25,0,0,0,0,0.5,0,0,0,0,1])).to.equal(true);
    });
    it('#translate', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        left.translate([1,2,3]);
        expect(left.equal([1,0.5,0,1,0,0.5,0,0,0,0,1,0,1,1.5,3,2])).to.equal(true);
    });
    it('#scale', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        left.scale([1,2,3]);
        expect(left.equal([1, 0.5, 0, 1, 0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#rotate', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        left.rotate(Math.PI/3,[1,0,0]);
        expect(left.equal([1, 0.5, 0, 1, 0, 0.25, 0.8660253882408142, 0, 0, -0.4330126941204071, 0.5, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#rotateX', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        left.rotateX(Math.PI/3);
        expect(left.equal([1, 0.5, 0, 1, 0, 0.25, 0.8660253882408142, 0, 0, -0.4330126941204071, 0.5, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#rotateY', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        left.rotateY(Math.PI/3);
        expect(left.equal([0.5, 0.25, -0.8660253882408142, 0.5, 0, 0.5, 0, 0, 0.8660253882408142, 0.4330126941204071, 0.5, 0.8660253882408142, 0, 0, 0, 1])).to.equal(true);
    });
    it('#rotateZ', function () {
        let left=new Mat4([1,0.5,0,1,0,0.5,0,0,0,0,1,0,0,0,0,1]);
        left.rotateZ(Math.PI/3);
        expect(left.equal([0.5, 0.6830127239227295, 0, 0.5, -0.8660253882408142, -0.1830127090215683, 0, -0.8660253882408142, 0, 0, 1, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromQuat', function () {
        let left=new Mat4();
        let quat=new Quat([0.4,0.4,0.4,1]);
        left.fromQuat(quat);
        expect(left.equal([.35999998450279236, 1.1200000047683716, -0.47999998927116394, 0, -0.47999998927116394, 0.35999998450279236, 1.1200000047683716, 0, 1.1200000047683716, -0.47999998927116394, 0.35999998450279236, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromXRotation', function () {
        let left=new Mat4();
        left.fromXRotation(Math.PI/3);
        expect(left.equal([1, 0, 0, 0, 0, 0.5, 0.8660253882408142, 0, 0, -0.8660253882408142, 0.5, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromYRotation', function () {
        let left=new Mat4();
        left.fromYRotation(Math.PI/3);
        expect(left.equal([0.5, 0, -0.8660253882408142, 0, 0, 1, 0, 0, 0.8660253882408142, 0, 0.5, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromZRotation', function () {
        let left=new Mat4();
        left.fromZRotation(Math.PI/3);
        expect(left.equal([0.5, 0.8660253882408142, 0, 0, -0.8660253882408142, 0.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromRotation', function () {
        let left=new Mat4();
        left.fromRotation(Math.PI/3,[1,1,1]);
        expect(left.equal([0.6666666865348816, 0.6666666865348816, -0.3333333432674408, 0, -0.3333333432674408, 0.6666666865348816, 0.6666666865348816, 0, 0.6666666865348816, -0.3333333432674408, 0.6666666865348816, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromTranslation', function () {
        let left=new Mat4();
        left.fromTranslation([1,1,1]);
        expect(left.equal([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1])).to.equal(true);
    });
    it('#fromScaling', function () {
        let left=new Mat4();
        left.fromScaling([2,1,2]);
        expect(left.equal([2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1])).to.equal(true);
    });
    it('#fromRotationTranslation', function () {
        let left=new Mat4();
        let quat=new Quat([0.4,0.4,0.4,1]);
        left.fromRotationTranslation(quat,[2,1,2]);
        expect(left.equal([0.35999998450279236, 1.1200000047683716, -0.47999998927116394, 0, -0.47999998927116394, 0.35999998450279236, 1.1200000047683716, 0, 1.1200000047683716, -0.47999998927116394, 0.35999998450279236, 0, 2, 1, 2, 1])).to.equal(true);
    });
    it('#getTranslation', function () {
        let left=new Mat4();
        let quat=new Quat([0.4,0.4,0.4,1]);
        left.fromRotationTranslation(quat,[2,1,2]);
        expect(left.getTranslation().equal([2,1,2])).to.equal(true);
    });
    it('#getScaling', function () {
        let left=new Mat4();
        let quat=new Quat([0.4,0.4,0.4,1]);
        left.fromRotationTranslation(quat,[2,1,2]);
        let q=left.getScaling();      
        expect(q.equal([1.2705904245376587, 1.2705904245376587, 1.2705904245376587])).to.equal(true);
    });
    it('#getRotation', function () {
        let left=new Mat4();
        let quat=new Quat([0.4,0.4,0.4,1]);
        left.fromRotationTranslation(quat,[2,1,2]);
        let q=left.getRotation();      
        expect(q.equal([0.46291229128837585, 0.46291229128837585, 0.46291229128837585, 0.6800732612609863])).to.equal(true);
    });
    it('#compose', function () {
        let left=new Mat4();
        let quat=new Quat([0.4,0.4,0.4,1]);
        left.compose([10,2,4],quat,[2,1,2]);    
        expect(left.equal([.7199999690055847, 2.240000009536743, -0.9599999785423279, 0, -0.47999998927116394, 0.35999998450279236, 1.1200000047683716, 0, 2.240000009536743, -0.9599999785423279, 0.7199999690055847, 0, 10, 2, 4, 1])).to.equal(true);
    });
    it('#decompose', function () {
        let left=new Mat4([.7199999690055847, 2.240000009536743, -0.9599999785423279, 0, -0.47999998927116394, 0.35999998450279236, 1.1200000047683716, 0, 2.240000009536743, -0.9599999785423279, 0.7199999690055847, 0, 10, 2, 4, 1]);
        let {trans,scale,rotation}=left.decompose(); 
        expect(trans.equal([10, 2, 4])).to.equal(true);
        expect(scale.equal([2.5411808490753174, 1.2705904245376587, 2.5411808490753174])).to.equal(true);
        expect(rotation.equal([0.43976667523384094, 0.46291229128837585, 0.7175140380859375, 0.6800732612609863])).to.equal(true);
    });
    it('#lookAt', function () {
        let c=new Mat4();
        c.lookAt([10,5,10],[0,0,0]);
        expect(c.equal([0.7071067690849304, -0.2357022613286972, 0.6666666865348816, 0, 0, 0.9428090453147888, 0.3333333432674408, 0, -0.7071067690849304, -0.2357022613286972, 0.6666666865348816, 0, -0, -0, -15, 1])).to.equal(true);
    });
    it('#targetTo', function () {
        let c=new Mat4();
        c.targetTo([10,5,10],[0,0,0]);
        expect(c.equal([0.7071067690849304, 0, -0.7071067690849304, 0, -0.2357022613286972, 0.9428090453147888, -0.2357022613286972, 0, 0.6666666865348816, 0.3333333432674408, 0.6666666865348816, 0, 10, 5, 10, 1])).to.equal(true);
    });
    it('#perspective', function () {
        let c=new Mat4();
        c.perspective(Math.PI/3,1,1,100);
        expect(c.equal([1.7320507764816284, 0, 0, 0, 0, 1.7320507764816284, 0, 0, 0, 0, -1.0101009607315063, -1, 0, 0, -1.0101009607315063, 0])).to.equal(true);
    });
    it('#ortho', function () {
        let c=new Mat4();
        c.ortho(1,10,1,10,1,100);
        expect(c.equal([0.2222222238779068, 0, 0, 0, 0, 0.2222222238779068, 0, 0, 0, 0, -0.010101010091602802, 0, -1.2222222089767456, -1.2222222089767456, -0.010101010091602802, 1])).to.equal(true);
    });
    it('#frustum', function () {
        let c=new Mat4();
        c.frustum(1,10,1,10,1,100);
        expect(c.equal([.2222222238779068, 0, 0, 0, 0, 0.2222222238779068, 0, 0, 1.2222222089767456, 1.2222222089767456, -1.0202020406723022, -1, 0, 0, -2.0202019214630127, 0])).to.equal(true);
    });
    it('#toString', function () {
        let c=new Mat4();
        expect(c.toString()).to.equal('Mat4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)');
    });
})